const mongoose = require('mongoose')

const Image = mongoose.model('Image')
const Comment = mongoose.model('Comment')

const statsfn = () => {
  const imagePromise = Image.aggregate()
    .group({
      _id: null,
      numOfImages: {$sum: 1},
      numOfLikes: {$sum: "$likes"},
      numOfVisits: {$sum: "$visit"}
    })
    .then(arr => arr[0])
  const commentPromise = Comment.countDocuments()
  return Promise.all([imagePromise,commentPromise])
    .then(
        ([{numOfImages,numOfLikes,numOfVisits}, numOfComments]) => ({
          numOfImages,numOfLikes,numOfVisits,numOfComments
        })
    )
}

module.exports = statsfn